{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 两种方法\n",
    "import keras\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from keras.utils import np_utils\n",
    "\n",
    "from keras.datasets import mnist\n",
    "# Sequential按顺序构成的模型\n",
    "from keras.models import Sequential\n",
    "# Dense全连接层\n",
    "from keras.layers import Dense,Activation,Dropout,Convolution2D,MaxPool2D,Flatten\n",
    "\n",
    "# RNN的模型\n",
    "from keras.layers import SimpleRNN,LSTM,GRU\n",
    "# 优化器\n",
    "from keras.optimizers import SGD,Adam\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n",
    "# 载入数据\n",
    "(x_train,y_train),(x_test,y_test)=mnist.load_data()\n",
    "\n",
    "print('xtrain',x_train.shape)\n",
    "print('ytrain',y_train.shape)\n",
    "\n",
    "# -1是自动转换，255.0是为了归一化\n",
    "# (60000, 28, 28)->(60000, 28,28,1)转化形式\n",
    "x_train=x_train/255.0\n",
    "x_test=x_test/255.0\n",
    "\n",
    "print('xtrain',x_train.shape)\n",
    "print('xtest',x_test.shape)\n",
    "\n",
    "#序列长度 一共28行\n",
    "time_steps=28\n",
    "# 数据长度 一行28个元素\n",
    "input_size=28\n",
    "#隐藏层 size 50\n",
    "cell_size=50\n",
    "\n",
    "# 转化成one-hot格式\n",
    "y_train=np_utils.to_categorical(y_train,num_classes=10)\n",
    "y_test=np_utils.to_categorical(y_test,num_classes=10)\n",
    "\n",
    "# 创建模型,输入784个神经元，输出10个神经元\n",
    "model=Sequential(\n",
    "# [Dense(units=10,input_dim=784,bias_initializer='one',activation='softmax')]\n",
    ")\n",
    "# 第一个卷积层\n",
    "model.add(\n",
    "SimpleRNN(\n",
    "units=cell_size # 输出\n",
    ")\n",
    ")\n",
    "model.add(\n",
    "MaxPool2D(\n",
    "pool_size=2, strides=2, padding='same'\n",
    ")\n",
    ")\n",
    "# 第二个卷积层\n",
    "model.add(\n",
    "Convolution2D(\n",
    "64,\n",
    "5,\n",
    "strides=1,\n",
    "padding='same',\n",
    "activation='relu'\n",
    ")\n",
    ")\n",
    "model.add(\n",
    "MaxPool2D(\n",
    "2,2, 'same'\n",
    ")\n",
    ")\n",
    "model.add(Flatten())\n",
    "model.add(\n",
    "Dense(1024,activation='relu')\n",
    ")\n",
    "model.add(\n",
    "Dropout(0.4)\n",
    ")\n",
    "model.add(\n",
    "Dense(10,activation='softmax')\n",
    ")\n",
    "\n",
    "\n",
    "# 分类模型使用交叉熵速度比较快\n",
    "# sgd=SGD(lr=0.3)\n",
    "adam=Adam(lr=1e-4)\n",
    "\n",
    "model.compile(optimizer=adam,loss='categorical_crossentropy',metrics=['accuracy'])\n",
    "\n",
    "# 训练\n",
    "# batch_size=32一次训练32章，epochs 训练10轮，把所有的图片都训练一次\n",
    "model.fit(x_train,y_train,batch_size=32,epochs=10)\n",
    "#评估模型\n",
    "loss,accuray=model.evaluate(x_test,y_test)\n",
    "\n",
    "print('\\test loss',loss)\n",
    "print('accuray',accuray)\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
